home *** CD-ROM | disk | FTP | other *** search
/ Over 1,000 Windows 95 Programs / Over 1000 Windows 95 Programs (Microforum) (Disc 1).iso / 1244 / procapp3.c < prev    next >
C/C++ Source or Header  |  1996-06-17  |  8KB  |  261 lines

  1. #include <windows.h>
  2. #include <malloc.h>
  3. #include "procbox.h"                               
  4.  
  5. ////////////////////////////////////////////////////////////////////////////////
  6. //
  7. //    PROCAPP3.C    - Demonstrates use of Process Box Low-Level Interface
  8. //                    
  9. //                    - Contains code for 
  10. //                            - WinMain Entry Point
  11. //                            - WndProc
  12.  
  13.  
  14. HINSTANCE hinst;
  15. typedef struct _tagUserData
  16. {
  17.      FARPROC    lpfnPBCallback;
  18.      WORD        process_count;     
  19. } USERDATA;             // data kept by each process box
  20.  
  21. int    iProcBoxCount = 0;        // incremented for each new process box
  22. BOOL    bProcessLoop = FALSE;   // any active process boxes?
  23.                                         // If there are, we enter a special loop in 
  24.                                         //        WinMain....
  25. LRESULT FAR PASCAL _export WndProc(HWND, UINT, WPARAM, LPARAM);
  26. int CALLBACK _export PBCallback(HWND, int , LPARAM );
  27.                
  28. char        szAppName[] = "PROCAPP";
  29. char        szWindowText[] = "ProcessBox - Low-Level Interface Demo";
  30.  
  31. /////////////////////////////////////////////////
  32. //
  33. //
  34. //     WndProc     
  35. //
  36.  
  37. LRESULT FAR PASCAL _export WndProc (HWND hwnd, UINT message, 
  38.                                                 WPARAM wParam, LPARAM lParam)
  39. {      
  40.     HWND        hwndPB;
  41.     USERDATA *pUserData;
  42.     char        szText[64];
  43.                 
  44.     switch (message)
  45.     {          
  46.         case WM_KEYDOWN:                
  47.         case WM_LBUTTONDOWN:                                             
  48.             ////////////////////////
  49.             //    
  50.             //    CREATE PROCESSBOX:
  51.             //               
  52.             wsprintf(szText, "Process #%d in progress", iProcBoxCount);            
  53.            hwndPB = CreateProcessBox(    hwnd,         // owner
  54.                                         szText,     // caption
  55.                                         NULL,        // message                                        
  56.                                         10+iProcBoxCount*4,    // x screen coord
  57.                                         50+iProcBoxCount*4,        // y screen coord
  58.                                         NULL );  
  59.                                                                          
  60.             if (hwndPB)     // If Process Box was created OK
  61.             {
  62.                             // pUserData = a chunk of Data private to each process box
  63.                 pUserData = (USERDATA*)malloc (sizeof(USERDATA));    
  64.                             // pUserData is freed in PBCallback
  65.                 if (pUserData)
  66.                 {
  67.                     pUserData->process_count = 0;
  68.                     pUserData->lpfnPBCallback = MakeProcInstance((FARPROC)PBCallback, hinst);    
  69.                                     // the proc-instance is freed in PBCallback also
  70.                                     
  71.                     if (pUserData->lpfnPBCallback)                                                
  72.                         AttachProcess(hwndPB, pUserData->lpfnPBCallback, (LPARAM)(void far *)pUserData);        
  73.                     else
  74.                     {
  75.                         MessageBox(hwnd, "Unable to Attach Process Callback", NULL, NULL);
  76.                         DestroyProcessBox(hwndPB);
  77.                     }
  78.                 }                                    
  79.                 iProcBoxCount++;
  80.                 bProcessLoop = TRUE;    // enable the process loop
  81.             }
  82.             else
  83.                 MessageBox(hwnd, "Unable to create Process Box", NULL, NULL);
  84.                 
  85.         return 0;        
  86.  
  87.         case WM_DESTROY:                                               
  88.             PostQuitMessage(0);            
  89.         return 0;        
  90.     }
  91.    return DefWindowProc (hwnd, message, wParam, lParam );
  92. }              
  93. ///////////////////////////////////////////////////////////
  94. //
  95. //    Process Box Callback
  96. //                               
  97.  
  98. int CALLBACK _export PBCallback(HWND hwndPB, int iCode, LPARAM lParam)
  99. {
  100.     USERDATA far *lpUserData = (USERDATA far *)lParam;    
  101.     long        j;
  102.         
  103.     switch (iCode)
  104.     {
  105.         case PBC_OPEN:
  106.             lpUserData->process_count = 0;
  107.         return TRUE;    
  108.         
  109.         case PBC_CLOSE:        
  110.             if (lpUserData)    // double check, just in case!
  111.             {
  112.                 FreeProcInstance(lpUserData->lpfnPBCallback);
  113.                 _ffree(lpUserData);    // free up the user data (allocated during Process Box creation)
  114.             }
  115.         return TRUE;
  116.         
  117.         case PBC_CANCEL:
  118.         return (    MessageBox(NULL, "Really cancel operation?", "Message Box", 
  119.                         MB_APPLMODAL|MB_YESNO)==IDYES ? TRUE : FALSE );
  120.                             
  121.             
  122.         case PBC_PROCESS:       
  123.             lpUserData->process_count++;
  124.  
  125.             for (j=0; j<0xEFF; j++);    // a delay
  126.  
  127.             if ((lpUserData->process_count)>10000)
  128.                 return PBCR_END;                            
  129.             
  130.             SendMessage(hwndPB, PM_SETGAUGE, lpUserData->process_count/100, 0l);
  131.             return PBCR_CONTINUE;
  132.     }
  133.     return TRUE;
  134. }
  135.  
  136.  
  137.  
  138. /////////////////////////////////////////////////
  139. //
  140. //
  141. //     WinMain - Entry Point
  142. //
  143. //
  144. int PASCAL WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  145.                     LPSTR lpszCmdParam, int nCmdShow)
  146. {     
  147.        
  148.     MSG             msg ;
  149.     WNDCLASS        wndclass ;       
  150.     HWND                hwnd;
  151.     HWND                hwndPB;    // handle to a ProcessBox
  152.     int                iState;     // return parameter from IterateTaskProcesses
  153.                                                
  154.     hinst = hInstance;        
  155.     if (!hPrevInstance) 
  156.     {
  157.         wndclass.style         = NULL ;
  158.         wndclass.lpfnWndProc   = WndProc ;
  159.         wndclass.cbClsExtra    = 0 ;
  160.         wndclass.cbWndExtra    = 0 ;
  161.         wndclass.hInstance     = hInstance ;
  162.         wndclass.hIcon         = LoadIcon (NULL,IDI_APPLICATION) ;
  163.         wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  164.         wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  165.         wndclass.lpszMenuName  = NULL ;
  166.         wndclass.lpszClassName = szAppName ;
  167.                 
  168.         RegisterClass (&wndclass) ;
  169.     }
  170.         
  171.     hwnd = CreateWindow (    szAppName,             // class
  172.                                      szWindowText,            // window text
  173.                                     WS_OVERLAPPEDWINDOW,            // style
  174.                                     CW_USEDEFAULT, CW_USEDEFAULT,        // x, y start
  175.                                     CW_USEDEFAULT, CW_USEDEFAULT,        // width, height
  176.                                     NULL,             // parent window handle
  177.                                     NULL,             // menu handle
  178.                                     hInstance,         // instance handle
  179.                                     NULL) ;            // long pointer to creation data
  180.  
  181.     ShowWindow (hwnd, nCmdShow) ;
  182.     UpdateWindow (hwnd) ;
  183.         // THE STANDARD MESSAGE LOOP           
  184.     while (GetMessage (&msg, NULL, 0, 0))
  185.     {
  186.         TranslateMessage (&msg) ;
  187.         DispatchMessage (&msg) ;
  188.         
  189.         // END OF STANDARD MESSAGE LOOP
  190.         
  191.         //                
  192.         // PROCESSING LOOP: Add this to enable Process Boxes to act like modal dialogs
  193.         //
  194.         while (bProcessLoop)    // this is set in WndProc case WM_LBUTTONDOWN:
  195.         {                  
  196.             if (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE))
  197.             {
  198.                 if (msg.message == WM_QUIT)
  199.                     return msg.wParam;
  200.                     
  201.                 if (!IsProcessMessage(NULL, &msg)) // add IsDialogMessage here if you have modeless dialogs
  202.                 {
  203.                      TranslateMessage(&msg);
  204.                      DispatchMessage(&msg);
  205.                 }
  206.             } // end if (PeekMessage)                
  207.             
  208.             hwndPB = IterateTaskProcesses(&iState);    // This is where processing for all the
  209.                                                                     // Process Boxes occurs
  210.                                                                     //
  211.                                                                     // * see the comments below 
  212.             
  213.             if (hwndPB==NULL)                    // no more Process Boxes
  214.                 bProcessLoop = FALSE;        // exit the processing loop
  215.             else if (iState!=PI_CONTINUE)    // This hwndPB returned PI_END,PI_ERROR or PI_CANCEL from IterateTaskProcesses
  216.                 DestroyProcessBox(hwndPB);    // -> Just destroy it
  217.             
  218.         }// end while (bProcessLoop)
  219.         
  220.         //
  221.         //    END OF PROCESSING LOOP
  222.         //
  223.     }    // end while (GetMessage)
  224.     
  225.     return msg.wParam ;   
  226. }  
  227. //
  228. //    !!!!!!!!!!! NOT USED !!!!!!!!!!!!!!!!!!!!!!!!
  229. //        FOR EXPLANATION ONLY:
  230. //
  231. //    * RE: IterateTaskProcesses - this function is basically a wrapper around
  232. //                a GetProcessBox(First/Next), IterateProcess loop:
  233. //
  234. //        The code below is functionally equivalent to a call IterateTaskProcesses
  235. //        
  236. HWND YOUR_CUSTOM_IterateTaskProcesses(int far *lpState)
  237. {
  238.     HWND        hwndPB, hwndPBReturn;    
  239.            
  240.     hwndPB = GetProcessBoxFirst();    // find the first Process Box owned by this task
  241.     
  242.     if (hwndPB)
  243.         hwndPBReturn = hwndPB;
  244.     
  245.     while (hwndPB)        // step through the list of Process Boxes owned by this task
  246.     {             
  247.         *lpState = IterateProcess(hwndPB);        // check for cancelation, send PBC_PROCESS code to callback
  248.                                                             // returns value =
  249.                                                             //        PI_PROCESSERROR, PI_CANCEL,
  250.                                                             //        PI_END, or PI_CONTINUE 
  251.         if (*lpState != PI_CONTINUE)
  252.             break;
  253.                 
  254.         hwndPB = GetProcessBoxNext(hwndPB);    // get next process box owned by this task
  255.         
  256.         if (hwndPB)
  257.             hwndPBReturn = hwndPB;
  258.     }
  259.     
  260.     return hwndPBReturn;
  261. }